package site.marqstree.kotlin.usercenter.ui.activity.forgetpwd

import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_forget_pwd.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import site.marqstree.kotlin.library.base.extent.enable
import site.marqstree.kotlin.library.base.ui.activity.BaseMvpActivity
import site.marqstree.kotlin.usercenter.R
import site.marqstree.kotlin.usercenter.injection.component.DaggerUserComponent
import site.marqstree.kotlin.usercenter.ui.activity.resetpwd.ResetPwdActivity


/*
 * 项目名: KotlinMall
 * 包名: site.marqstree.kotlin.usercenter.ui.activity
 * 文件名: ForgetPwdActivity
 * 创建者: marqstree
 * 创建时间: 2020/2/22 16:40
 * 描述: TODO
 */
/*
    忘记密码界面
 */
class ForgetPwdActivity : BaseMvpActivity<ForgetPwdPresenter>(),
    ForgetPwdView, View.OnClickListener {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_forget_pwd)

        initView()

    }

    /*
        初始化视图
     */
    private fun initView() {

        mNextBtn.enable(mMobileEt,{isBtnEnable()})
        mNextBtn.enable(mVerifyCodeEt,{isBtnEnable()})

        mVerifyCodeBtn.setOnClickListener(this)
        mNextBtn.setOnClickListener(this)
    }

    override fun initBusinessInjection() {
        DaggerUserComponent
            .builder()
            .activityComponent(activityComponent)
            .build()
            .inject(this)
        mPresenter.mView = this
    }

    /*
        点击事件
     */
    override fun onClick(view: View) {
        when(view.id){
            R.id.mVerifyCodeBtn -> {
                mVerifyCodeBtn.requestSendVerifyNumber()
                toast("发送验证成功")
            }

            R.id.mNextBtn -> {
                mPresenter.forgetPwd(mMobileEt.text.toString(),mVerifyCodeEt.text.toString())
            }
        }
    }

    /*
        判断按钮是否可用
     */
    private fun isBtnEnable():Boolean{
        return mMobileEt.text.isNullOrEmpty().not() &&
                mVerifyCodeEt.text.isNullOrEmpty().not()
    }

    /*
        忘记密码回调
     */
    override fun onForgetPwdResult(result: String) {
        toast(result)
        startActivity<ResetPwdActivity>("mobile"  to mMobileEt.text.toString())
        finish()
    }
}